﻿using Swimj.DomainLib.Entities;

namespace Production.DomainLib.AggregatesModel.StandardBomAggregate
{
    public class StandardBom : AuditableAggregateRoot<int>
    {
        /// <summary>
        /// BOM编码
        /// </summary>
        public string? Code { get; set; }

        /// <summary>
        /// BOM名称
        /// </summary>
        public string? Name { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public string? Version { get; set; }

        /// <summary>
        /// 合同号
        /// </summary>
        public string? Contract { get; set; }

        public int ProductId { get; set; }

        public string? ProductCode { get; set; }

        public string? ProductName { get; set; }

        public double Quality { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string? Description { get; set; }

        public IList<StandardBomDetail>? Items { get; set; }

        public StandardBom()
        {
            Items = new List<StandardBomDetail>();
        }

        public StandardBom(string? code, string? name, string? version,
            string? contract, int productId, string? productCode, string? productName,
            double quality, string? description) : this()
        {
            Code = code;
            Name = name;
            Version = version;
            Contract = contract;
            ProductId = productId;
            ProductCode = productCode;
            ProductName = productName;
            Quality = quality;
            Description = description;
        }

        public void AddItem(int materialKey, string? materialCode, string? materialName, double itemQuality,
            double itemLossRate)
        {
            Items?.Add(new StandardBomDetail(materialKey, materialCode, materialName, itemQuality, itemLossRate));
        }

        public void Update(string? name, string? version,
            string? contract, int productId, string? productCode, string? productName,
            double quality, string? description)
        {
            Name = name;
            Version = version;
            Contract = contract;
            ProductId = productId;
            ProductCode = productCode;
            ProductName = productName;
            Quality = quality;
            Description = description;
        }

        public void UpdateItem(int id, int materialKey, string? materialCode, string? materialName, double itemQuality,
            double itemLossRate)
        {
            var standardBom = Items?.FirstOrDefault(i => i.Id == id);
            standardBom?.Update(materialKey, materialCode, materialName, itemQuality, itemLossRate);
        }

        public void RemoveItem(StandardBomDetail item)
        {
            Items?.Remove(item);
        }
    }
}